package com.example.springbootredis.controller;

import com.example.springbootredis.entity.User;
import com.example.springbootredis.util.BaseResp;
import com.example.springbootredis.util.TestDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Api("redis【string】")
@RestController
@RequestMapping("/redis/string")
public class RedisStringController {

    @Autowired
    RedisTemplate redisTemplate;
    //-----------------redis string start-------------
    @ApiOperation("初始化数据")
    @RequestMapping(value = "/initData",method = RequestMethod.GET)
    public BaseResp string_initData(){
        log.info("string_initData start");
        for (User user : TestDataUtil.getUsers()) {
            log.info("string_initData user:{}",user);
            redisTemplate.opsForValue().set(user.getId().toString(),user);
        }
        log.info("string_initData end");
        return BaseResp.buildSuccess();
    }

    @ApiOperation("get方法")
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    public BaseResp string_get(@RequestParam("strKey")String strKey){
        Object val = redisTemplate.opsForValue().get(strKey);
        return BaseResp.buildDataSuccess(val);
    }

    @ApiOperation("set方法")
    @RequestMapping(value = "/string/set",method = RequestMethod.GET)
    public BaseResp string_set(@RequestParam("strKey")String strKey,@RequestParam("strVal")String strVal){
        redisTemplate.opsForValue().set(strKey,strVal);
        return BaseResp.buildSuccess();
    }
    //-----------------redis string end-------------

}
